package com.qianfeng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.dto.UserInfo;
import com.qianfeng.entity.Role;
import com.qianfeng.entity.User;
import com.qianfeng.entity.UserRole;
import com.qianfeng.mapper.RoleMapper;
import com.qianfeng.mapper.UserMapper;
import com.qianfeng.mapper.UserRoleMapper;
import com.qianfeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Transactional
    @Override
    public Page getUserList(Integer pageNo,Integer pageSize) {
            int pageNo_=1;
            int pageSize_=2;
        if (pageNo!=null){
            pageNo_=pageNo;
        }
        if (pageSize!=null){
            pageSize_= pageSize;
        }
        PageHelper.startPage(pageNo_,pageSize_);
        List<User> userList = userMapper.getUserList();
        PageInfo pageInfo = new PageInfo(userList);
        Page page = new Page();
        page.setPageCount(pageInfo.getPages());
        page.setHasNext(pageInfo.isHasNextPage());
        List<UserInfo> userInfoList = new ArrayList<>();
        for (User user : userList) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId(user.getUserId());
            userInfo.setUserName(user.getUserName());
            userInfo.setUserLoginName(user.getUserLoginName());
            userInfo.setUserPassword(user.getUserPassword());
            userInfo.setUserEmail(user.getUserEmail());
            userInfo.setCreateTime(user.getCreateTime());
            userInfo.setUpdateTime(user.getUpdateTime());
            userInfo.setUserState(user.getUserState());
            List<Integer> roleIds = new ArrayList<>();
            List<UserRole> userRoles = userRoleMapper.getUserRoles(user.getUserId());
            for (UserRole userRole : userRoles) {
                roleIds.add(userRole.getRoleId());
            }
            //批量查询
            if (roleIds.size()>0){
                List<Role> roles = roleMapper.batchGetRoles(roleIds);
                userInfo.setRoles(roles);
            }
            userInfoList.add(userInfo);
        }
        page.setObj(userInfoList);
        return page;
    }

    @Transactional
    @Override
    public void addUser(User user,Integer roleId) {
        userMapper.addUser(user);
        System.out.println(user.getUserId());
        UserRole userRole = new UserRole();
        userRole.setUserId(user.getUserId());
        userRole.setRoleId(roleId);
        userRoleMapper.addUserRole(userRole);
    }

    @Transactional
    @Override
    public void deleteUser(Integer userId) {
        userMapper.deleteUser(userId);
        userRoleMapper.deleteUserRole(userId);
    }
    @Transactional
    @Override
    public void updateUser(User user,Integer roleId) {
        userMapper.updateUser(user);
        userRoleMapper.deleteUserRole(user.getUserId());
        UserRole userRole = new UserRole();
        userRole.setUserId(user.getUserId());
        userRole.setRoleId(roleId);
        userRoleMapper.addUserRole(userRole);
    }

    @Override
    public User getUserByUserId(Integer userId) {
        User userByUserId = userMapper.getUserByUserId(userId);
        return userByUserId;
    }

    @Override
    public List<UserInfo> getUserLikeList(String userName) {
        List<UserInfo> userInfoList = new ArrayList<>();
        List<User> userList = userMapper.getLikeUserList(userName);
        for (User user : userList) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId(user.getUserId());
            userInfo.setUserLoginName(user.getUserLoginName());
            userInfo.setUserPassword(user.getUserPassword());
            userInfo.setUserEmail(user.getUserEmail());
            userInfo.setUserName(user.getUserName());
            userInfo.setCreateTime(user.getCreateTime());
            userInfo.setUpdateTime(user.getUpdateTime());
            userInfo.setUserState(user.getUserState());
            List<Integer> roleIds = new ArrayList<>();
            List<UserRole> userRoles = userRoleMapper.getUserRoles(user.getUserId());
            for (UserRole userRole : userRoles) {
                roleIds.add(userRole.getRoleId());
            }
            //批量查询
            List<Role> roleList = roleMapper.batchGetRoles(roleIds);
            userInfo.setRoles(roleList);
            userInfoList.add(userInfo);
        }
        return userInfoList;
    }

    @Override
    public User getUserByUserName(String userName) {
        User user = userMapper.getUserByUserName(userName);
        return user;
    }


}
